package com.elder.business.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import com.alibaba.fastjson2.JSONObject;
import com.elder.business.domain.*;
import com.elder.business.mapper.ApartmentDictMapper;
import com.elder.business.mapper.MemberInfoMapper;
import com.elder.business.mapper.NurseContentMapper;
import com.elder.business.mapper.ReceptionNoticeMapper;
import com.elder.business.service.IReceptionNoticeService;
import com.elder.common.core.page.TableDataInfo;
import com.elder.common.utils.DateUtils;
import com.elder.common.utils.IntegerUtils;
import com.elder.common.utils.LongUtils;
import com.elder.common.utils.StringNewUtils;
import com.elder.common.utils.bean.BeanUtils;
import com.elder.common.utils.uuid.SnowGenerator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 接待通知Service业务层处理
 * 
 * @author elder
 * @date 2024-03-13
 */
@Service
public class ReceptionNoticeServiceImpl implements IReceptionNoticeService
{
    @Autowired
    private ReceptionNoticeMapper receptionNoticeMapper;
    @Autowired
    private NurseContentMapper nurseContentMapper;
    @Autowired
    private ApartmentDictMapper apartmentDictMapper;
    @Autowired
    private MemberInfoMapper memberInfoMapper;

    /**
     * 查询接待通知
     * 
     * @param id 接待通知主键
     * @return 接待通知
     */
    @Override
    public ReceptionNoticeDTO selectReceptionNoticeById(Long id) {
        ReceptionNotice v = receptionNoticeMapper.selectReceptionNoticeById(id);
        ReceptionNoticeDTO dto = new ReceptionNoticeDTO();
        BeanUtils.copyProperties(v, dto);
        dto.setId(StringNewUtils.formatStr(v.getId()));
        dto.setOrderId(StringNewUtils.formatStr(v.getOrderId()));
        dto.setOrderTime(DateUtils.dateStrMIAO(v.getOrderTime()));
        dto.setLiveTime(Objects.isNull(DateUtils.dateStr(v.getLiveTime()))?"0":DateUtils.dateStr(v.getLiveTime()));
        return dto;
    }

    /**
     * 查询接待通知列表
     *
     * @return 接待通知
     */
    @Override
    public TableDataInfo selectReceptionNoticeList(Long userId,String elderName,String startTime,String endTime,Integer pageNum,Integer pageSize)
    {
        Long start = null;
        Long end = null;
        if(!StringNewUtils.isEmpty(startTime) && !StringNewUtils.isEmpty(endTime)){
            start = DateUtils.getDayStartTime(DateUtils.getTargetDateSeconds(startTime));
            end = DateUtils.getDayEndTime(DateUtils.getTargetDateSeconds(endTime));
        }
        MemberInfo member = memberInfoMapper.getMember(userId);
        PageHelper.startPage(pageNum,pageSize);
        List<ReceptionNotice> list = receptionNoticeMapper.selectReceptionNoticeList(member.getMemberId(),elderName, start, end);
        PageInfo<ReceptionNotice> pageInfo = new PageInfo<>(list);
        List<ReceptionNotice> receptionNoticeList = pageInfo.getList();
        List<ReceptionNoticeDTO> receptionNoticeDTOList = receptionNoticeList.stream().map(v -> {
            ReceptionNoticeDTO dto = new ReceptionNoticeDTO();
            BeanUtils.copyProperties(v, dto);
            dto.setId(StringNewUtils.formatStr(v.getId()));
            dto.setOrderId(StringNewUtils.formatStr(v.getOrderId()));
            dto.setOrderTime(DateUtils.dateStrMIAO(v.getOrderTime()));
            dto.setLiveTime(DateUtils.dateStr(v.getLiveTime()));
            return dto;
        }).collect(Collectors.toList());
        TableDataInfo data = new TableDataInfo();
        data.setCode(200);
        data.setMsg("success");
        data.setRows(receptionNoticeDTOList);
        data.setTotal(pageInfo.getTotal());
        return data;
    }

    /**
     * 新增接待通知
     * 
     * @param receptionNotice 接待通知
     * @return 结果
     */
    @Override
    public int insertReceptionNotice(ReceptionNotice receptionNotice)
    {
        List<ApartmentDict> dictInfo = apartmentDictMapper.getApartmentDictInfo(1);
        Map<Integer, String> roomMap = dictInfo.stream().collect(Collectors.toMap(ApartmentDict::getCode, ApartmentDict::getName));
        String nurseContents = receptionNotice.getNurseContents();
        List<String> serviceIds = Arrays.asList(nurseContents.split(","));
        List<Long> serviceIdList = serviceIds.stream().map(Long::parseLong).collect(Collectors.toList());
        List<NurseContent> contentList = nurseContentMapper.getNurseContentList(serviceIdList);
        List<String> nurseNames = contentList.stream().map(NurseContent::getNurseName).collect(Collectors.toList());
        String nurseContent = String.join(",", nurseNames);

        receptionNotice.setId(SnowGenerator.getId());
        receptionNotice.setNurseContents(nurseContent);
        if(!StringNewUtils.isEmpty(receptionNotice.getRoomType())){
            receptionNotice.setRoomType(roomMap.get(IntegerUtils.toInteger(receptionNotice.getRoomType())));
        }
        receptionNotice.setStatus(false);
        return receptionNoticeMapper.insertReceptionNotice(receptionNotice);
    }

    /**
     * 修改接待通知
     * 
     * @param receptionNotice 接待通知
     * @return 结果
     */
    @Override
    public int updateReceptionNotice(ReceptionNotice receptionNotice)
    {
        return receptionNoticeMapper.updateReceptionNotice(receptionNotice);
    }

    /**
     * 批量删除接待通知
     * 
     * @param ids 需要删除的接待通知主键
     * @return 结果
     */
    @Override
    public int deleteReceptionNoticeByIds(Long[] ids)
    {
        return receptionNoticeMapper.deleteReceptionNoticeByIds(ids);
    }

    /**
     * 删除接待通知信息
     * 
     * @param id 接待通知主键
     * @return 结果
     */
    @Override
    public int deleteReceptionNoticeById(Long id)
    {
        return receptionNoticeMapper.deleteReceptionNoticeById(id);
    }

    @Override
    public int updateReceptionNoticeState(ReceptionNoticeDTO receptionNotice) {
        return receptionNoticeMapper.updateReceptionNoticeState(LongUtils.str2Long(receptionNotice.getId()), receptionNotice.getStatus());
    }
}
